package iotwisp.school.utils;

import org.apache.commons.lang3.StringUtils;

public class PaymentOrderIdUtil {

	/**
	 * 
	 * @param orderId
	 *            2018070609530100000001
	 * @param lastPaymentOrderId
	 *            2018070609530100000001001
	 * @return
	 */
	public static String generatePaymentOrderId(String orderId, String lastPaymentOrderId) {

		if (StringUtils.isBlank(lastPaymentOrderId)) {
			return orderId + "001";
		} else {
			String current = StringUtils.substring(lastPaymentOrderId, 22);
			Integer nextValue = Integer.valueOf(current) + 1;
			String before = "";
			String next = String.valueOf(Integer.valueOf(current) + 1);
			if ((nextValue / 10) < 1) {
				before += "00";
			} else if ((nextValue / 100) < 1) {
				before += "0";
			}
			return orderId + before + next;
		}
	}

	public static void main(String[] args) {
		String id1 = PaymentOrderIdUtil.generatePaymentOrderId("2018070609530100000001", null);
		System.out.println(id1);

		String id2 = PaymentOrderIdUtil.generatePaymentOrderId("2018070609530100000001", "2018070609530100000001021");
		System.out.println(id2);
		
		String id3 = PaymentOrderIdUtil.generatePaymentOrderId("2018070609530100000001", "2018070609530100000001010");
		System.out.println(id3);
	}
}
